﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;

namespace UDS.SubModule.UnitiveDocument.DocumentFlowPages
{
    /// <summary>
    /// BangdingRole 的摘要说明。
    /// </summary>
    public partial class BangdingRolePage : Page
    {
        protected int TacheID
        {
            get;
            private set;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            TacheID = Request.QueryString["TacheID"] == "" ? 0 : Int32.Parse(Request.QueryString["TacheID"]);
            if (!Page.IsPostBack)
            {
                Bangding();
            }
        }

        private void InitCurRole()
        {
            var mySQL = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@TacheID", SqlDbType.Int, 4, TacheID)
            };

            using (var dr = mySQL.RunDataReader("dbo.sproc_Flow_GetBangdingRole", parameters))
            {
                lstCurRole.Items.Clear();
                while (dr.Read())
                {
                    lstCurRole.Items.Add(new ListItem(dr["role_name"].ToString(), ((int) dr["role_id"]).ToString()));
                }
            }
        }

        private void InitAllRole()
        {
            var mySQL = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@TacheID", SqlDbType.Int, 4, TacheID)
            };

            using (var dr = mySQL.RunDataReader("dbo.sproc_Flow_GetNotBangdingRole", parameters))
            {
                lstAllRole.Items.Clear();
                while (dr.Read())
                {
                    lstAllRole.Items.Add(new ListItem(dr["role_name"].ToString(), ((int) dr["role_id"]).ToString()));
                }
            }
        }

        private void InitCurPosition()
        {
            var mySQL = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@TacheID", SqlDbType.Int, 4, TacheID)
            };

            using (var dr = mySQL.RunDataReader("dbo.sproc_Flow_GetBangdingPosition", parameters))
            {
                lstCurPosition.Items.Clear();
                while (dr.Read())
                {
                    lstCurPosition.Items.Add(new ListItem(dr["Position_name"].ToString(), dr["Position_id"].ToString()));
                }
            }
        }

        private void InitAllPosition()
        {
            var mySQL = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@TacheID", SqlDbType.Int, 4, TacheID)
            };

            using (var dr = mySQL.RunDataReader("dbo.sproc_Flow_GetNotBangdingPosition", parameters))
            {
                lstAllPosition.Items.Clear();
                while (dr.Read())
                {
                    lstAllPosition.Items.Add(new ListItem(dr["Position_name"].ToString(), dr["Position_id"].ToString()));
                }
            }
        }

        private void InitCurTeam()
        {
            var mySQL = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@TacheID", SqlDbType.Int, 4, TacheID)
            };

            using (var dr = mySQL.RunDataReader("dbo.sproc_Flow_GetBangdingTeam", parameters))
            {
                lstCurTeam.Items.Clear();
                while (dr.Read())
                {
                    lstCurTeam.Items.Add(new ListItem(dr["Team_name"].ToString(), dr["Team_id"].ToString()));
                }
            }
        }

        private void InitAllTeam()
        {
            var mySQL = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@TacheID", SqlDbType.Int, 4, TacheID)
            };

            using (var dr = mySQL.RunDataReader("dbo.sproc_Flow_GetNotBangdingTeam", parameters))
            {
                lstAllTeam.Items.Clear();
                while (dr.Read())
                {
                    lstAllTeam.Items.Add(new ListItem(dr["Team_name"].ToString(), dr["Team_id"].ToString()));
                }
            }
        }

        private void InitCurStaff()
        {
            var mySQL = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@TacheID", SqlDbType.Int, 4, TacheID)
            };

            using (var dr = mySQL.RunDataReader("dbo.sproc_Flow_GetBangdingStaff", parameters))
            {
                lstCurStaff.Items.Clear();
                while (dr.Read())
                {
                    lstCurStaff.Items.Add(new ListItem(dr["Staff_name"].ToString(), dr["Staff_id"].ToString()));
                }
            }
        }

        private void InitAllStaff()
        {
            var mySQL = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@TacheID", SqlDbType.Int, 4, TacheID)
            };

            using (var dr = mySQL.RunDataReader("dbo.sproc_Flow_GetNotBangdingStaff", parameters))
            {
                lstAllStaff.Items.Clear();
                while (dr.Read())
                {
                    lstAllStaff.Items.Add(new ListItem(dr["Staff_name"].ToString(), dr["Staff_id"].ToString()));
                }
            }
        }

        private void cmdAdd_Click(object sender, EventArgs e)
        {
            if (lstAllRole.SelectedIndex >= 0)
            {
                var mySQL = new Database();
                var parameters = new[]
                {
                    Database.MakeInParam("@TacheID", SqlDbType.Int, 4, TacheID),
                    Database.MakeInParam("@RoleID", SqlDbType.Int, 4, Int32.Parse(lstAllRole.Items[lstAllRole.SelectedIndex].Value))
                };

                mySQL.RunProc("dbo.sproc_Flow_AddBangdingRole", parameters);

                //Response.Write("<script language='javascript'>alert('绑定成功！');</script>");
                Bangding();
            }
        }

        private void cmdDelete_Click(object sender, EventArgs e)
        {
            if (lstCurRole.SelectedIndex >= 0)
            {
                var mySQL = new Database();

                var parameters = new[]
                {
                    Database.MakeInParam("@TacheID", SqlDbType.Int, 4, TacheID),
                    Database.MakeInParam("@RoleID", SqlDbType.Int, 4, Int32.Parse(lstCurRole.Items[lstCurRole.SelectedIndex].Value))
                };

                mySQL.RunProc("dbo.sproc_Flow_DeleteBangdingRole", parameters);

                //Response.Write("<script language='javascript'>alert('取消绑定成功！');</script>");		
                Bangding();
            }
        }

        private void lstAllRole_SelectedIndexChanged(object sender, EventArgs e)
        {}

        private void cmdReturn_Click(object sender, EventArgs e)
        {
            Server.Transfer("ManageTache.aspx");
        }

        private void cmdAddPositon_Click(object sender, EventArgs e)
        {
            if (lstAllPosition.SelectedIndex >= 0)
            {
                var mySQL = new Database();

                var parameters = new[]
                {
                    Database.MakeInParam("@TacheID", SqlDbType.Int, 4, TacheID),
                    Database.MakeInParam("@PositionID", SqlDbType.Int, 4, Int32.Parse(lstAllPosition.Items[lstAllPosition.SelectedIndex].Value))
                };

                mySQL.RunProc("dbo.sproc_Flow_AddBangdingPosition", parameters);

                Bangding();
            }
        }

        private void cmdDeletePosition_Click(object sender, EventArgs e)
        {
            if (lstCurPosition.SelectedIndex >= 0)
            {
                var mySQL = new Database();

                var parameters = new[]
                {
                    Database.MakeInParam("@TacheID", SqlDbType.Int, 4, TacheID),
                    Database.MakeInParam("@PositionID", SqlDbType.Int, 4, Int32.Parse(lstCurPosition.Items[lstCurPosition.SelectedIndex].Value))
                };

                mySQL.RunProc("dbo.sproc_Flow_DeleteBangdingPosition", parameters);

                //Response.Write("<script language='javascript'>alert('取消绑定成功！');</script>");		
                Bangding();
            }
        }

        private void cmdDeleteTeam_Click(object sender, EventArgs e)
        {
            if (lstCurTeam.SelectedIndex >= 0)
            {
                var mySQL = new Database();

                var parameters = new[]
                {
                    Database.MakeInParam("@TacheID", SqlDbType.Int, 4, TacheID),
                    Database.MakeInParam("@TeamID", SqlDbType.Int, 4, Int32.Parse(lstCurTeam.Items[lstCurTeam.SelectedIndex].Value))
                };

                mySQL.RunProc("dbo.sproc_Flow_DeleteBangdingTeam", parameters);

                //Response.Write("<script language='javascript'>alert('取消绑定成功！');</script>");		
                Bangding();
            }
        }

        private void cmdDeleteStaff_Click(object sender, EventArgs e)
        {
            if (lstCurStaff.SelectedIndex >= 0)
            {
                var mySQL = new Database();

                var parameters = new[]
                {
                    Database.MakeInParam("@TacheID", SqlDbType.Int, 4, TacheID),
                    Database.MakeInParam("@StaffID", SqlDbType.Int, 4, Int32.Parse(lstCurStaff.Items[lstCurStaff.SelectedIndex].Value))
                };

                mySQL.RunProc("dbo.sproc_Flow_DeleteBangdingStaff", parameters);

                //Response.Write("<script language='javascript'>alert('取消绑定成功！');</script>");		
                Bangding();
            }
        }

        private void cmdAddTeam_Click(object sender, EventArgs e)
        {
            if (lstAllTeam.SelectedIndex >= 0)
            {
                var mySQL = new Database();

                var parameters = new[]
                {
                    Database.MakeInParam("@TacheID", SqlDbType.Int, 4, TacheID),
                    Database.MakeInParam("@TeamID", SqlDbType.Int, 4, Int32.Parse(lstAllTeam.Items[lstAllTeam.SelectedIndex].Value))
                };

                mySQL.RunProc("dbo.sproc_Flow_AddBangdingTeam", parameters);

                Bangding();
            }
        }

        private void cmdAddStaff_Click(object sender, EventArgs e)
        {
            if (lstAllStaff.SelectedIndex >= 0)
            {
                var mySQL = new Database();

                var parameters = new[]
                {
                    Database.MakeInParam("@TacheID", SqlDbType.Int, 4, TacheID),
                    Database.MakeInParam("@StaffID", SqlDbType.Int, 4, Int32.Parse(lstAllStaff.Items[lstAllStaff.SelectedIndex].Value))
                };

                mySQL.RunProc("dbo.sproc_Flow_AddBangdingStaff", parameters);

                Bangding();
            }
        }

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN：该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.cmdAddStaff.Click += new System.EventHandler(this.cmdAddStaff_Click);
            this.cmdDeleteStaff.Click += new System.EventHandler(this.cmdDeleteStaff_Click);
            this.cmdDeleteTeam.Click += new System.EventHandler(this.cmdDeleteTeam_Click);
            this.cmdAddTeam.Click += new System.EventHandler(this.cmdAddTeam_Click);
            this.lstAllRole.SelectedIndexChanged += new System.EventHandler(this.lstAllRole_SelectedIndexChanged);
            this.cmdAdd.Click += new System.EventHandler(this.cmdAdd_Click);
            this.cmdDelete.Click += new System.EventHandler(this.cmdDelete_Click);
            this.cmdReturn.Click += new System.EventHandler(this.cmdReturn_Click);
            this.cmdAddPositon.Click += new System.EventHandler(this.cmdAddPositon_Click);
            this.cmdDeletePosition.Click += new System.EventHandler(this.cmdDeletePosition_Click);
            this.Load += new System.EventHandler(this.Page_Load);
        }

        #endregion

        #region 绑定数据库

        private void Bangding()
        {
            InitCurRole();
            InitAllRole();

            InitCurPosition();
            InitAllPosition();

            InitCurTeam();
            InitAllTeam();

            InitCurStaff();
            InitAllStaff();
        }

        #endregion
    }
}